using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

using Telerik.WebControls;
using NCS.Library;
using NCS.Library.Components.Commands;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;

namespace Portal.Administrator.UserControls
{
	public partial class NavPanel : AuthenticatedControl
	{
        private string cmd = string.Empty;
        private int portalid = 0;
        private string path = string.Empty;
        string nodePath = string.Empty;

        public TreeNodeCollection nodelist = new TreeNodeCollection();

        private void GetPath()
        {
            path = "|";
            ArrayList titleArr = new ArrayList();

            if (cmd != string.Empty)
            {
                CommandInfo curCmd = CommandController.GetCommand(cmd);
                while (curCmd != null)
                {
                    path += curCmd.CommandID + "|";
                    titleArr.Add(curCmd.CommandName);
                    curCmd = CommandController.GetCommand(curCmd.CommandParentID);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                LoadMenu();
            }
        }

        private void LoadMenu()
        {
            //rpMenu.Items.Clear();
            GetPath();
            cmd = ConvertUtility.ToString(Request.QueryString["cmd"]);
            //portalid = ConvertUtility.ToInt32(Request.QueryString["pid"]);

            portalid = ConvertUtility.ToInt32(Request.QueryString["portalid"]);
            DataTable dtCommands = null;

            if (!CurrentUser.IsSuperAdmin)
            {
                dtCommands = CommandController.GetCommandsForUserByPortalID(CurrentUser.UserID, portalid, false);
            }
            else
            {
                dtCommands = CommandController.GetCommands(false);
            }

            DataRow[] drRoots = dtCommands.Select("CommandParentID = 0");

            foreach (DataRow row in drRoots)
            {
                //RadPanelItem rootItem = new RadPanelItem();
                TreeNode node = new TreeNode();
                //rootItem.Text = row["CommandName"].ToString();
                node.Text = row["CommandName"].ToString();
                //rootItem.Value = row["CommandKey"].ToString();
                node.Value = row["CommandKey"].ToString();
                if (row["CommandUrl"].ToString() != string.Empty)
                {
                    //rootItem.NavigateUrl = row["CommandUrl"].ToString();
                    node.NavigateUrl = row["CommandUrl"].ToString();
                }
                else if (ConvertUtility.ToString(row["CommandKey"]).Trim() != string.Empty)
                {
                    //rootItem.NavigateUrl = AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                    //rootItem.NavigateUrl = CPEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                    node.NavigateUrl = AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                }
                else
                {
                    node.NavigateUrl = "#";
                }

                if ((bool)row["CommandVisible"] == false || (bool)row["CommandEnable"] == false)
                {
                    continue;
                }

                //rpMenu.Items.Add(rootItem);

                //rootItem.ImageUrl = "/RadControls/Panelbar/Skins/Default/Img/Collapsed.gif";
                //rootItem.ExpandedImageUrl = "/RadControls/Panelbar/Skins/Default/Img/Expanded.gif";
                //rootItem.ImageOverUrl = "/RadControls/Panelbar/Skins/Default/Img/Collapsed.gif";
                //rootItem.Expanded = false;
                //rootItem.CssClass = "text7";
                //LoadCommandItems(rootItem, 0, dtCommands, (int)row["CommandID"]);
                LoadCommandItems(node.ChildNodes, 0, dtCommands, (int)row["CommandID"]);
                this.nodelist.Add(node);
            }
        }

        //private void LoadCommandItems(RadPanelItem curItem, int level, DataTable dtCommands, int id)
        private void LoadCommandItems(TreeNodeCollection curItem, int level, DataTable dtCommands, int id)
        {
            DataRow[] drChilds = dtCommands.Select("CommandParentID = " + id + " AND CommandVisible = 1");
            if (level > 0)
            {
                //curItem.ImageUrl = drChilds.Length > 0 ? "/RadControls/Panelbar/Skins/Default/Img/ItemCollapsed.gif" : "/RadControls/Panelbar/Skins/Default/Img/ItemExpanded.gif";
                //curItem.ExpandedImageUrl = drChilds.Length > 0 ? "/RadControls/Panelbar/Skins/Default/Img/ItemExpanded.gif" : "/RadControls/Panelbar/Skins/Default/Img/ItemCollapsed.gif";
            }

            foreach (DataRow row in drChilds)
            {
                //RadPanelItem item = new RadPanelItem();
                TreeNode node = new TreeNode();
                //item.Text = "<font color=\"#434343\">" + row["CommandName"].ToString() + "</font>";
                node.Text = row["CommandName"].ToString();
                //item.Value = row["CommandKey"].ToString();
                node.Value = row["CommandKey"].ToString();
                //if (item.Value == Request.QueryString["cmd"])
                //    item.Text = "<b>" + item.Text + "</b>";
                if (row["CommandUrl"].ToString() != string.Empty)
                {
                    //item.NavigateUrl = row["CommandUrl"].ToString();
                    node.NavigateUrl = row["CommandUrl"].ToString();
                }
                else if (row["CommandKey"].ToString() != string.Empty)
                {
                    //item.NavigateUrl = AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                    //item.NavigateUrl = CPEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                    node.NavigateUrl = AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString();
                }
                else
                {
                    node.NavigateUrl = "#";
                }
                if ((bool)row["CommandVisible"] == false || (bool)row["CommandEnable"] == false)
                {
                    continue;
                }
                //curItem.Expanded = true;

                //curItem.Items.Add(item);
                LoadCommandItems(node.ChildNodes, level + 1, dtCommands, (int)row["CommandID"]);
                curItem.Add(node);
            }

        }

        protected string CreateMenuString(TreeNode node, int level)
        { 
            string strlevel = "";
            for(int i = 0; i < level; i++)
            {
                strlevel += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            level++;

            string html = "";

            if (node.ChildNodes.Count > 0)
            {
                if (level == 1)
                {
                    html += "<table width=\"100%\" >";
                }

                html += "<tbody id=\"body_" + level.ToString() + "\">";
                for (int j = 0; j < node.ChildNodes.Count; j++)
                {

                    html += "<tr>";
                    //html += "<th background=\"/images/HomePage/bg_left_menu_1.jpg\" scope=\"col\">&nbsp;</th>";
                    html += "<th scope=\"col\">&nbsp;</th>";
                    html += "<th height=\"27\" scope=\"col\">";
                    html += "<a href=\"" + node.ChildNodes[j].NavigateUrl + "\" class=\"text2\">";
                    if (level == 1)
                    {
                        html += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + strlevel + node.ChildNodes[j].Text + "";
                        html += "<tr>";
                        html += "<th scope=\"col\">";
                        html += "</th>";
                        html += "<th background=\"/images/HomePage/line1.gif\" scope=\"col\">";
                        html += "    <img src=\"/images/HomePage/blank.gif\" width=\"1\" height=\"1\" /></th>";
                        html += "<th scope=\"col\">";
                        html += "</th>";
                        html += "</tr>";
                    }
                    else
                    {
                        html += strlevel + node.ChildNodes[j].Text;
                    }
                    html += "</a>";
                    html += "</th>";
                    //html += "<th background=\"/images/HomePage/bg_left_menu_2.jpg\" scope=\"col\">&nbsp;</th>";
                    html += "</tr>";
                    if (node.ChildNodes[j].ChildNodes.Count > 0)
                    {
                        html += CreateMenuString(node.ChildNodes[j], level);
                    }

                }
                html += "</tbody>";
                if (level == 1)
                {
                    html += "</table>";
                }
            }
            return html;
        }
	}
}